package com.dsteiner.gwt.cafeteriasophia.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import com.dsteiner.gwt.cafeteriasophia.client.domain.ShiftChange;
import com.dsteiner.gwt.cafeteriasophia.client.event.AddShiftChangeEvent;
import com.dsteiner.gwt.cafeteriasophia.client.event.CheckShiftChangeEvent;
import com.dsteiner.gwt.cafeteriasophia.client.event.NavEvent;
import com.dsteiner.gwt.cafeteriasophia.client.model.NavModel;
import com.dsteiner.gwt.cafeteriasophia.client.model.ShiftChangeModel;
import com.dsteiner.gwt.cafeteriasophia.client.rpc.DataService;
import com.dsteiner.widgets.client.DateUtilImpl;
import com.dsteiner.widgets.client.JsUtil;
import com.dsteiner.widgets.client.Widgets.IButton;
import com.dsteiner.widgets.client.Widgets.IHTML;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.domain.Sort;
import com.dsteiner.widgets.client.event.EventListener;
import com.dsteiner.widgets.client.event.EventManager;
import com.dsteiner.widgets.client.event.EventManagerImpl;
import com.dsteiner.widgets.client.rpc.CrudResult;
import com.dsteiner.widgets.client.rpc.SearchRequest;
import com.dsteiner.widgets.client.rpc.SearchResult;
import com.dsteiner.widgets.client.rpc.SearchRequest.SearchRequestImpl;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class ShiftChangeController {

	private ShiftChangeModel shiftChangeModel;
	private IHTML titleHtml;


	public void registerEventManager(EventManager eventManager) {
		eventManager.registerListener(AddShiftChangeEvent.class, new EventListener<AddShiftChangeEvent>(){
			public void onEvent(AddShiftChangeEvent event) {
				ShiftChange shiftChange = event.getShiftChange();
				DataService.Util.getInstance().createModelObject(shiftChange, new AsyncCallback<CrudResult<ShiftChange>>(){
					public void onFailure(Throwable caught) {
						caught.printStackTrace();
					}
					public void onSuccess(CrudResult<ShiftChange> result) {
						if(result.getError() != null){
							JsUtil.getInstance().showAlert(result.getError());
							return;
						}
						JsUtil.getInstance().showAlert("Cambio de turno guardado exitosamente");
						EventManagerImpl.getInstance().fireEvent(new NavEvent(NavModel.PAGE_ID_NAV));
					}
				});
			}
		});
	}

	public void registerNavLink(IHTML navLink) {
		navLink.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				EventManagerImpl.getInstance().fireEvent(new NavEvent(NavModel.PAGE_ID_NAV));
			}
		});
	}

	public void registerSaveButton(IButton saveButton) {
		saveButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				EventManagerImpl.getInstance().fireEvent(new CheckShiftChangeEvent());
			}
		});
	}

	public void registerShiftChangeModel(ShiftChangeModel shiftChangeModel) {
		this.shiftChangeModel = shiftChangeModel;
		shiftChangeModel.addPropertyChangeListener("lastShiftChange", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateTitle();
			}
		});
	}
	public void registerShiftChangeWidget(ILazyWidget shiftChangeWidget) {
		shiftChangeWidget.addWidgetListener(new WidgetListenerAdapter() {
			@Override
			public void onAttach(IWidget widget) {
				shiftChangeModel.setShiftChange(new ShiftChange());
				SearchRequestImpl searchRequest = new SearchRequestImpl();
				searchRequest.setClassName(ShiftChange.class.getName());
				searchRequest.setLimit(1);
				searchRequest.setOffset(0);
				searchRequest.setSort(new Sort("date", false));
				searchRequest.setType(SearchRequest.TYPE_ALL);
				DataService.Util.getInstance().search(searchRequest, new AsyncCallback<SearchResult<ShiftChange>>(){
					public void onFailure(Throwable caught) {
					}
					public void onSuccess(SearchResult<ShiftChange> result) {
						List<ShiftChange> objects = result.getObjects();
						System.out.println("loading former shift change:"+objects);
						if(!objects.isEmpty())
							shiftChangeModel.setLastShiftChange(objects.get(0));
					}
				});
			}
		});
	}

	public void registerTitleHtml(IHTML titleHtml) {
		this.titleHtml = titleHtml;
	}

	private void updateTitle() {
		ShiftChange lastShiftChange = shiftChangeModel.getLastShiftChange();
		if(lastShiftChange != null){
			titleHtml.setHTML("Turno del "+DateUtilImpl.getInstance().formatDateSpanish(lastShiftChange.getDate()) 
					+ " al "+DateUtilImpl.getInstance().formatDateSpanish(shiftChangeModel.getShiftChange().getDate()));
		}
	}

}